//
//  SEInquirySettingFeeCell.swift
//  SmartEyeForDoctor
//
//  Created by qdsg on 2021/5/5.
//

import UIKit

struct SEInquirySettingFeeCellModel {
    var title: String = ""
    var placeHolder: String = ""
    var text: String = ""
    var unit: String = ""
    var cellType: SEInquirySettingFeeCellType = .normal
}

enum SEInquirySettingFeeCellType {
    case normal
    case unit
    case code
}

protocol SEInquirySettingFeeCellDelegate: NSObjectProtocol {
    func inquirySettingFeeCelldidEndEdit(cell: SEInquirySettingFeeCell, indexPath: IndexPath)
}

class SEInquirySettingFeeCell: UITableViewCell {

    @IBOutlet weak var bgView: UIView! {
        didSet {
            bgView.layer.cornerRadius = 4
            bgView.layer.borderColor = UIColor(hex: "#E8E8E8").cgColor
            bgView.layer.borderWidth = 1
        }
    }
    @IBOutlet weak var codeBtn: UIButton!{
        didSet {
            codeBtn.layer.cornerRadius = codeBtn.frame.size.height / 2.0
            codeBtn.layer.masksToBounds = true
        }
    }
    @IBOutlet weak var unitLabel: UILabel!
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var textField: UITextField! {
        didSet { textField.delegate = self }
    }

    @IBOutlet weak var textFieldTrailingSuperView: NSLayoutConstraint!
    @IBOutlet weak var textFieldTrailingCodeBtn: NSLayoutConstraint!
    @IBOutlet weak var textFieldTrailingUnitLabel: NSLayoutConstraint!

    @IBAction func codeBtnAction(_ sender: UIButton) {
    }


    private var cellType: SEInquirySettingFeeCellType = .normal {
        didSet {
            textFieldTrailingSuperView.priority = .defaultLow
            textFieldTrailingUnitLabel.priority = .defaultLow
            textFieldTrailingCodeBtn.priority = .defaultLow
            switch cellType {
            case .normal:
                unitLabel.isHidden = true
                codeBtn.isHidden = true
                textFieldTrailingSuperView.priority = .defaultHigh
            case .unit:
                unitLabel.isHidden = false
                codeBtn.isHidden = true
                textFieldTrailingUnitLabel.priority = .defaultHigh
            case .code:
                unitLabel.isHidden = true
                codeBtn.isHidden = false
                textFieldTrailingCodeBtn.priority = .defaultHigh
            }
        }
    }

    weak var delegate: SEInquirySettingFeeCellDelegate?
    var indexPath = IndexPath(row: 0, section: 0)

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    func updateData(_ model: SEInquirySettingFeeCellModel, indexPath: IndexPath) {
        self.indexPath = indexPath
        cellType = model.cellType
        titleLabel.text = model.title
        textField.placeholder = model.placeHolder
        textField.text = model.text
        unitLabel.text = model.unit
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}

extension SEInquirySettingFeeCell: UITextFieldDelegate {
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        guard string == "." || string == "0" else {
            let new = (textField.text! as NSString).replacingCharacters(in: range, with: string)
            let expression =  "^[0-9]*((\\.|,)[0-9]{0,2})?$"
            let regex = try! NSRegularExpression(pattern: expression, options: .allowCommentsAndWhitespace)
            let numberOfMatches = regex.numberOfMatches(in: new, options: .reportProgress, range: NSRange(location: 0, length: new.count))
            return numberOfMatches != 0
        }
        
        guard let text = textField.text else { return true }
        
        if text.range(of: ".") != nil && string == "." {
            return false
        }
        
        if text.range(of: ".") != nil {
            let list = text.components(separatedBy: ".")
            return list.last?.count ?? 0 < 2
        }
        
        SELog("text = \(textField.text!) \n string = \(string)")
        
        return true
    }

    func textFieldDidEndEditing(_ textField: UITextField) {
        delegate?.inquirySettingFeeCelldidEndEdit(cell: self, indexPath: indexPath)
    }
}
